<?php
/* SVN FILE: $Id: performance.ctp 36 2009-10-22 21:25:27Z kenreily@gmail.com $ */
/**
 * Short description for file.
 *
 * Long description for file
 *
 * PHP versions 4 and 5
 *
 * Recenseo: Online submission and reviewing for education
 * Copyright 2008-2009, Ken Reily
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * $Date: 2009-10-22 21:25:27 +0000 (Thu, 22 Oct 2009) $
 * $Revision: 36 $
 * $Author: kenreily@gmail.com $
 * $HeadURL: http://recenseo.googlecode.com/svn/trunk/app/views/users/performance.ctp $
 */

  if (isset($data))
  {
?>
<h4>Reviewer Performance Report</h4>
<p>&nbsp;</p>
<?php
    // If the $ref array is set, then we know we are constructing a leaderboard.
    if (isset($ref))
    {
?>
<h5>Summary (vs. Top Peer Reviewer in Each Category)</h5>
<table>
<?php
    echo $html->tableHeaders(array('','Test Cases','Written Feedback (chars)','Author Rating','Peer Rating','Responsiveness (days)','Accuracy'));

    $stats = array('avg_test_cases' => 1,
                   'avg_feedback_chars' => 0,
                   'author_feedback_score' => 2,
                   'peer_feedback_score' => 2,
                   'responsiveness' => 1,
                   'accuracy' => 1);

    foreach ($data as $user)
    {
      if ($user['User']['id'] == $session->read('Auth.User.id'))
      {
        $top = array('Top Reviewer');
        $cells = array('<strong>You</strong>');
        $diff = array('Difference');
    
        foreach ($stats as $stat => $precision)
        {
          if (array_key_exists($stat,$user['UserStatistic']) && !empty($user['UserStatistic'][$stat]))
          {
	    if (!empty($ref[$stat]))
            {
	      $top[] = number_format($ref[$stat],$precision);
	    }
            else
            {
              $top[] = '-';
            }
	  
            if (!empty($user['UserStatistic'][$stat]))
            {
              $cells[] = $html->tag('strong',number_format($user['UserStatistic'][$stat],$precision));
            }
            else
            {
              $cells[] = '-';
            }

            if ($stat == 'accuracy')
            {
              $diff[] = array(number_format(abs($user['UserStatistic'][$stat]) - abs($ref[$stat]),$precision),
                              array('style' => 'color:red'));
            }
            else
            {
              $diff[] = 
                array(number_format($user['UserStatistic'][$stat] - $ref[$stat],$precision),
                      array('style' => 'color:red'));
            }
          }
          else
          {
	    $top[] = '-';
            $cells[] = '-';
	    $diff[] = '-';
          }
        }

	echo $html->tableCells($top);
        echo $html->tableCells($cells);
	echo $html->tableCells($diff);
      }
    }
?>
</table>

<h5>Detailed Rankings (Peers)</h5>
<table>
  <tr>
    <th></th>
    <th>Rank</th>
    <th>Total</th>
    <th>Test Cases</th>
    <th>Written Feedback (chars)</th>
    <th>Author Rating</th>
    <th>Peer Rating</th>
    <th>Responsiveness (days)</th>
    <th>Accuracy</th>
  </tr>
<?php
    $rank = 1;

    foreach ($data as $user)
    {
      $cells = array('&nbsp;',$rank++,'-');
    
      if ($user['User']['id'] == $session->read('Auth.User.id'))
      {
        $cells[0] = 'You ->';
      } 

      if (array_key_exists('Percentile',$user) && array_key_exists('total',$user['Percentile']))
      {
        $cells[2] = $html->div(null,
                            number_format($user['Percentile']['total'] * 100,1),
                            array('style' => 'background-color:red;width:' . $user['Percentile']['total'] * 200 . 'px;'));
      }

      foreach ($stats as $stat => $precision)
      {
        if (array_key_exists($stat,$user['UserStatistic']) && !empty($user['UserStatistic'][$stat]))
        {
          $cells[] = number_format($user['UserStatistic'][$stat],$precision);
        }
        else
        {
          $cells[] = '-';
        }
      }
   
      echo $html->tableCells($cells);
    }
?>
</table>
<?php
    }
    else
    {
?>
<h5>vs. TA</h5>
<table>
  <tr>
    <th></th>
    <th>Test Cases</th>
    <th>Written Feedback</th>
    <th>Author Rating</th>
    <th>Peer Rating</th>
    <th>Responsiveness</th>
    <th>Accuracy</th>
  </tr>
<?php

    $stats = array('avg_test_cases' => 1,
                   'avg_feedback_chars' => 0,
                   'author_feedback_score' => 2,
                   'peer_feedback_score' => 2,
                   'responsiveness' => 1,
                   'accuracy' => 1);

    $ta = array('Course TA');
    $user = array('You');
    $diff = array('Difference');

    foreach ($stats as $stat => $precision)
    {
      $ta_value = $data['ta']['UserStatistic'][$stat];
      $user_value = $data['user']['UserStatistic'][$stat];

      if ($stat == 'accuracy')
      {
        $diff_value = abs($user_value) - abs($ta_value);
      }
      else
      {
        $diff_value = $user_value - $ta_value;
      }

      $ta[] = number_format($ta_value,$precision);
      $user[] = number_format($user_value,$precision);

      if ((in_array($stat,array('responsiveness','accuracy')) && $diff_value > 0) ||
          (!in_array($stat,array('responsiveness','accuracy')) && $diff_value < 0))
      {
	$color = 'red';
      }
      else
      {
        $color = 'green';
      }

      $diff[] = $html->tag('font',number_format($diff_value,$precision),array('color' => $color));
    }

    echo $html->tableCells($ta);
    echo $html->tableCells($user);
    echo $html->tableCells($diff);
?>  
</table>
<?php
    }
?>
<h4>Legend</h4>
<dl>
  <dt>Test Cases</dt>
  <dd>Average # of test cases executed per review</dd>

  <dt>Written Feedback</dt>
  <dd>Average # of characters per review</dd>

  <dt>Author Rating</dt>
  <dd>Average numeric score (out of 5) given to your reviews by the submission author</dd>

  <dt>Peer Rating</dt>
  <dd>Average numeric score (out of 5) given to your reviews by objective peers</dd>

  <dt>Responsiveness</dt>
  <dd>Average # of days you take to complete a review (after assignment due date)</dd>

  <dt>Accuracy</dt>
  <dd>Numeric calculation (out of 100 points) of your reviews compared to the average of other reviewers who reviewed the same submissions.</dd>
</dl>
<?php
  }
?>
